<?php
class EmstrainingComponent extends Component {
    var $components = array('Zend');
    public function rand_string( $type ) {
        $return = "";
        $now = date("ymd");
        $key = array();
        $ticket = ClassRegistry::init("Ticket");
        $ids2 = $ticket->find('first',array('order'=>array('Ticket.created DESC')));
        $key[] = $ids2['Ticket']['key'];
        $main_key = array();
        foreach ($key as $value) {
            if($value){
                $val = (int) $value;
                if($val){
                    $date = substr($value, 0, 6);
                    if($date==$now){
                        $number = substr($value, -5);
                        $number = (int) $number;
                        $main_key[]=$number;
                    }else{
                        $main_key[] = 10000;
                    }
                }else{
                    $main_key[] = 10000;
                }
            }else{
                $main_key[] = 10000;
            }
        }
        $number = max($main_key);
        $number++;
        $return = "$now$number";
        return $return;
    }
    public function loyalty_voucher_number(){
        $rand=array();
        for($i=1;$i<=9;$i++){
            $rand[]=  rand(0, 9);
        }
        $rand = implode("", $rand);
        $rand ="2199$rand";
        $voucher = ClassRegistry::init("Voucher");
        $v = $voucher->findByCode($rand);
        if(!empty($v)){
            return $this->loyalty_voucher_number();
        }
        return $rand;
    }
    public function loyalty_gift_number(){
        $rand=array();
        for($i=1;$i<=9;$i++){
            $rand[]=  rand(0, 9);
        }
        $rand = implode("", $rand);
        $rand ="2199$rand";
        $voucher = ClassRegistry::init("Gift");
        $v = $voucher->findByCode($rand);
        if(!empty($v)){
            return $this->loyalty_gift_number();
        }
        return $rand;
    }
    public function generate_number($limit) {
        $rand=array();
        for($i=1;$i<=$limit;$i++){
            $rand[]=  rand(0, 9);
        }
        return implode("", $rand);
    }
    public function get_customer_number_code(){
        $rand=array();
        for($i=1;$i<=10;$i++){
            $rand[]=  rand(0, 9);
        }
        $rand = "96".implode("", $rand);
        $customer = ClassRegistry::init("Customer");
        $cus = $customer->find('first',array('conditions'=>array('customer_number_barcode'=>$rand)));
        if(!empty($cus)){
            return $this->get_customer_number_code();
        }
        return $rand;
    }
    public function generate_customer_number(&$customer_number,&$customer_number_barcode) {
        $rand = $this->get_customer_number_code();
        $customer_number_barcode = $rand;
        $this->Zend->import('Barcode');
        $this->Zend->include_barcode();
        $options=array(
            'text' => $customer_number_barcode,
            'font' => 5
        );
        $barcode = new Zend_Barcode_Object_Ean13($options);
        $customer_number = $barcode->getTextToDisplay();
    }
    public function writePdf($title,$html,$mode = "F") {
        $vendor = APP."/Plugin/CakePdf/Vendor/";
        require_once($vendor.'tcpdf/config/lang/eng.php');
        require_once($vendor.'tcpdf/mytcpdf.php');
        require_once($vendor.'tcpdf/htmlcolors.php');
        $pdf = new MYTCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
        $pdf->SetCreator(PDF_CREATOR);
        $pdf->SetAuthor('Long');
        // -----------------------------------------
        // set font
        $pdf->SetFont('dejavusans', '', 16);
        // add a page
        $pdf->AddPage();
        $pdf->writeHTML($html, true, false, true, false, '');
        // reset pointer to the last page
        $pdf->lastPage();
        $pdf->Output($title.'.pdf', $mode);
    }
}